<?php

class Services extends CI_Controller {

  public function searchpnr()
  {
  	if (!$this->session->userdata('logged_in')) {
		redirect('login');
	}
  	$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
	$this->form_validation->set_rules('pnr', 'PNR', 'required|min_length[6]|max_length[6]');
    $this->form_validation->set_rules('surname', 'Surname', 'required|min_length[2]|max_length[20]');
    $data['surname_not_match'] = FALSE;
    if ($this->form_validation->run() == FALSE)
    {
    	$this->template->write('title', 'PNR Görünlüte!');
	    $this->template->write_view('left_side_bar_top', 'search_flight_form');
	    //$this->template->write_view('left_side_bar_bottom', 'search_flight_left_bottom');
	    $this->template->write_view('content', 'view_pnr_content',$data);
	    $this->template->render();
    }
    else
    {
    	
    	$post_pnr = $this->commonlibrary->to_en_upper($this->input->post('pnr'));
    	$post_surname = $this->commonlibrary->to_en_upper($this->input->post('surname'));
    	$data['pnr_xml'] = $this->websdom_api->call_get_pnr($this->session->userdata('agency_tk_user'),$this->session->userdata('agency_tk_pass'),$post_pnr);
      	$exception = $data['pnr_xml']->xpath('//Exception');
      	$data['surname_not_match'] = FALSE;
		if (count($exception) == 0) {
			$surname = $data['pnr_xml']->xpath('//surname');
			$surname = (string)$surname[0]; 
			if ($surname == $post_surname)	{
				$this->template->write('title', 'PNR Görünlüte!');
			    $this->template->write_view('left_side_bar_top', 'search_flight_form');
			    //$this->template->write_view('left_side_bar_bottom', 'search_flight_left_bottom');
			    $this->template->write_view('content', 'view_pnr_content',$data);
			    $this->template->render();
			}
			else {
				$data['surname_not_match'] = TRUE;
				$data['pnr_xml'] = null;
				$this->template->write('title', 'PNR Görünlüte!');
		        $this->template->write_view('left_side_bar_top', 'search_flight_form');
		        $this->template->write_view('left_side_bar_bottom', 'content/callcenter');
		        $this->template->write_view('content', 'view_pnr_content',$data);
		        $this->template->render();	
				
			}
		}
		else { // exception göster
	      	$data['surname_not_match'] = TRUE;
			$this->template->write('title', 'PNR Görünlüte!');
	        $this->template->write_view('left_side_bar_top', 'search_flight_form');
	        $this->template->write_view('left_side_bar_bottom', 'content/callcenter');
	        $this->template->write_view('content', 'view_pnr_content',$data);
	        $this->template->render();	
		}		
	 } 
  }
  public function searchticket()
  {
  	if (!$this->session->userdata('logged_in')) {
		redirect('login');
	}
  	$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
	$this->form_validation->set_rules('ticket', 'Ticket', 'required|min_length[13]|max_length[13]');
    $this->form_validation->set_rules('surname', 'Surname', 'required|min_length[2]|max_length[20]');
    if ($this->form_validation->run() == FALSE)
    {
    	$this->template->write('title', 'Bilet Görüntüle!');
	    $this->template->write_view('left_side_bar_top', 'search_flight_form');
	    //$this->template->write_view('left_side_bar_bottom', 'search_flight_left_bottom');
	    $this->template->write_view('content', 'view_ticket_content');
	    $this->template->render();
    }
    else
    {
    	$post_surname = $this->commonlibrary->to_en_upper($this->input->get('surname'));
    	$data['ticket_xml'] = $this->websdom_api->call_get_eticket_info($this->session->userdata('agency_tk_user'),$this->session->userdata('agency_tk_pass'),$this->input->post('ticket'),$post_surname);
      	$this->template->write('title', 'Bilet Görüntület!');
        $this->template->write_view('left_side_bar_top', 'search_flight_form');
        $this->template->write_view('left_side_bar_bottom', 'content/callcenter');
        $this->template->write_view('content', 'view_ticket_content',$data);
        $this->template->render();
	 } 
  }
 public function cancelpnr()
  {
  	$data['surname_not_match'] = FALSE;
  	if (!$this->session->userdata('logged_in')) {
		redirect('login');
	}
	$post_pnr = $this->commonlibrary->to_en_upper($this->input->get('pnr'));
    $data['response_xml'] = $this->websdom_api->call_cancel_reservation($this->session->userdata('agency_tk_user'),$this->session->userdata('agency_tk_pass'),$post_pnr);
	$this->template->write('title', 'PNR Görünlüte!');
	$this->template->write_view('left_side_bar_top', 'search_flight_form');
	$this->template->write_view('left_side_bar_bottom', 'content/callcenter');
	$this->template->write_view('content', 'view_pnr_content',$data);
	$this->template->render();
	 
  }
}